<extend name='Index/index' />

<block name="main">
                <div class="bg-light lter b-b wrapper-md">
                    <h1 class="m-n font-thin h3">广告管理</h1>
                    <button style="margin-top: 20px;" class="btn btn-success" data-toggle="modal" data-target="#myModal_add">添加广告</button>
                </div>
                <div class="wrapper-md">
                    <div class="panel panel-default">
                        <div class="table-responsive">
                            <table class="table table-striped b-t b-light">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>广告名称</th>
                                        <th>链接</th>
                                        <th>配图</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <volist name='data' id='h'>
                                    <tr>
                                        <td><{$h.ad_id}></td>
                                        <td><{$h.ad_name}></td>
                                        <td><{$h.ad_link}></td>
                                        <td> <img width="200" height="130" src="__PUBLIC__/Uploads/ad/<{$h.ad_address}>" alt=""></td>
                                        <td>
                                            <if condition="$h.ad_status eq 0"><a class="ad_status">下线</a>
                                            <else /><a class="ad_status">上线</a>
                                            </if>
                                        </td>
                                        <td>
                                        <a class='btn btn-info' data-toggle="modal" data-target="#myModal" > 编辑</a> 
                                        <a id="<{$h.ad_id}>" class='btn btn-danger'>删除</a>
                                        </td>
                                    </tr>
                                    </volist>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- 添加英雄的模态框 -->
        <div class="modal fade" id="myModal_add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" id="add_close" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">添加广告</h4>
                </div>
                <form action="<{:U('Admin/ChatAd/add')}>"  method="POST" enctype= 'multipart/form-data'  class="form-horizontal" role="form">
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="inputEmail" class="col-sm-2 control-label">广告名称</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="adname" id="adname" value="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail" class="col-sm-2 control-label">广告链接</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="adlink" id="link" value="">
                            </div>
                        </div>
                         <div class="form-group">
                            <label for="inputphone" class="col-sm-2 control-label">广告图</label>
                            <div class="col-sm-9">
                                <input type="file" name="adpic" id="adpic" value="">
                            </div>
                        </div>
                     </div>
                    <div class="modal-footer">
                        <button id="close" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button id="add" type="submit" class="btn btn-primary">confirm</button>
                    </div>
            </form>
            </div>
        </div>
        </div>
<!-- 编辑英雄的模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" id="guanbi" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">编辑广告</h4>
                </div>
                <div class="modal-body">
                    
                    <form class="form-horizontal" role="form" action="<{:U('edit')}>" method="POST" enctype= 'multipart/form-data' >
                        <div class="form-group">
                            <label for="inputEmail" class="col-sm-2 control-label">ID</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name='ad_id' id="id" readonly="" value="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail" class="col-sm-2 control-label">广告名称</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="ad_name" id="ad_name" value="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail" class="col-sm-2 control-label">广告链接</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="ad_link" id="ad_link" value="">
                            </div>
                        </div>
                         <div class="form-group">
                            <label for="inputphone" class="col-sm-2 control-label">广告图</label>
                            <div class="col-sm-9">
                                <input type="file" name="adpic" id="adpic" value="">
                            </div>
                        </div>
                </div>
                <div class="modal-footer">
                    <button id="close" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button id="save" type="submit" class="btn btn-primary">Save changes</button>
                </div>
                    </form>
            </div>
        </div>
    </div>


<script type="text/javascript">
        $("a:contains('编辑')").click(function(){
            var ad_id = $(this).parents('tr').children('td').eq(0).html();
            var name = $(this).parents('tr').children('td').eq(1).html();
            var ad_link = $(this).parents('tr').children('td').eq(2).html();

            $('#id').val(ad_id);
            $('#ad_name').val(name);
            $('#ad_link').val(ad_link);

        })

            $("a:contains('删除')").click(function(){
                var id = $(this).parents('tr').children('td').eq(0).html();
                // alert(id);
                $(this).parents('tr').remove();
                $.ajax({
                url:"<{:U('del')}>",
                type:"POST",
                data:{'ad_id':id},
                dataType:'json', 
                success: function(data){
                    if(data == 1){
                        alert('删除成功');
                    }else{
                        alert('删除失败');
                    }
                    }
                })
            })
            $('.ad_status').click(function (){
                var str = $(this).html();
                var up = 0;
                var id = $(this).parents('tr').children('td').eq(0).html();
                alert(id);
                $('.ad_status').html('下线');
                // alert(1);
                if(str == '上线'){
                    $(this).html('下线');
                    up = 0;
                }else{
                    $(this).html('上线');
                    up = 1;
                }
                $.ajax({
                    url:"<{:U('e')}>",
                    type:"POST",
                    data:{'id':id,'up':up},
                    dataType:'json', 
                    
                });
            })
</script>
</block>
